***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file stores a bank-borrower dataset (useful to combine 2019 and 2015) and
* and sets up a dataset at the bank-province level
***********************************************************************************
***********************************************************************************

*==================================================================================
**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower)
run RO_creditregister tm(2019m2) tm(2020m2) 0
keep bankid borrowerid time disbur

merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e10 e14 e13 e12 dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

run RO_tdummies 2019 12

**# Bookmark (saving dataset - bank-borrower dataset)
compress
save $inputdirectory/INB_core2019temp, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower)
run RO_creditregister tm(2018m12) tm(2020m2) 0
keep bankid borrowerid time disbur comune_dip

merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

merge m:1 comune_dip using $inputdirectory/INB_comprov2019, keepus(prov)
keep if _merge!=2
drop _merge

collapse (sum) disbur, by(bankid borrowerid time prov)
duplicates tag bankid borrowerid time, g(dup)

egen xxx=max(disbur), by(bankid borrowerid time)
gen yyy=prov if disbur==xxx
egen zzz=max(yyy), by(bankid borrowerid time)
replace prov=zzz if dup>0
drop xxx yyy zzz
collapse (sum) disbur, by(bankid borrowerid time prov)

egen id=group(bankid borrowerid)
xtset id time

gen new=0
replace new=1 if L.id==.&L2.id==.&L3.id==.
gen old=1-new
gen disburold=disbur*old
drop old new 
keep if time>=tm(2019m2)&time<=tm(2020m2)

* Collapse by by bank-province
collapse (sum) disbur disburold, by(bankid time prov)

merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

* Compute some province (and bank-province) indicators
egen totdisburcom=sum(disbur), by(time prov)

gen ttt=disbur
replace ttt=0 if dtop66e14==0
egen totdistr66=sum(ttt), by(time prov)
egen zzz66=sum(ttt), by(time prov bankid)
drop ttt

gen ttt=disbur
replace ttt=0 if dmid6633e14==0
egen totdistr6633=sum(ttt), by(time prov)
egen zzz6633=sum(ttt), by(time prov bankid)
drop ttt

gen sharedis66=100*(totdistr66)/(totdisburcom)
gen sharedis6633=100*(totdistr6633)/(totdisburcom)
gen sharedis66R=100*(totdistr66-zzz66)/(totdisburcom-disbur)
gen sharedis6633R=100*(totdistr6633-zzz6633)/(totdisburcom-disbur)
replace sharedis66R=sharedis66 if sharedis66R==.
replace sharedis6633R=sharedis6633 if sharedis6633R==.

foreach x of var sharedis66 sharedis6633 {
gen xxx=`x' if time==tm(2019m8)
drop `x'
egen `x'=max(xxx), by(prov)
drop xxx
}

foreach x of var sharedis66R sharedis6633R {
gen xxx=`x' if time==tm(2019m8)
drop `x'
egen `x'=max(xxx), by(prov bankid)
drop xxx
}

gen interdis66=sharedis66*dtop66e14
gen interdisno66=sharedis66*(1-dtop66e14)
gen interdis66R=sharedis66R*dtop66e14
gen interdisno66R=sharedis66R*(1-dtop66e14)

* LINE ADDED (NOT NEEDED WITH THE ORIGINAL DATASETS)
drop if prov==.
egen id=group(bankid prov)
xtset id time

**# Bookmark (saving dataset - bank-province dataset)
compress
save $inputdirectory/INB_provbank2019, replace
